﻿Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports MobiminLib

Public Class HttpProcessor
    Dim MySocket As Socket
    Dim PhysicalFilePath As String
    Dim DefaultDocument As String
    Dim VirtualRoot As String

    Public Sub New(ByVal MySocket As Socket, ByVal PhysicalFilePath As String, ByVal DefaultDocument As String, ByVal VirtualRoot As String)
        Me.MySocket = MySocket
        Me.PhysicalFilePath = PhysicalFilePath
        Me.DefaultDocument = DefaultDocument
        Me.VirtualRoot = VirtualRoot
    End Sub

    Public Sub Processor()
        Dim Request As String
        Dim ListHeader As List(Of String)
        Dim bReceive() As Byte = New [Byte](4095) {}
        Dim ReceivedBytes As Integer = MySocket.Receive(bReceive, bReceive.Length, 0)
        Dim RecBuffer As String = Encoding.ASCII.GetString(bReceive, 0, ReceivedBytes)
        'find the GET request.
        If (RecBuffer.Substring(0, 3) <> "GET") Then 'Post
            Dim HeaderList = bReceive.Take(ReceivedBytes).ToList 'To prevent error with chrome (read twice)
            ReceivedBytes = MySocket.Receive(bReceive, bReceive.Length, 0)
            HeaderList.AddRange(bReceive.Take(ReceivedBytes))
            ProcessPost(HeaderList, MySocket, HeaderList.Count)
        Else
            ListHeader = RecBuffer.Replace(vbNewLine, " ").Split(" ").ToList

            'Get the Request
            Request = ListHeader(1)

            If Request.ToUpper.Contains("MOBIMIN-AJAX") Then
                'Special Ajax Request
                ProcessAjax(Request, MySocket)
            ElseIf Request.ToUpper.Contains("MOBIMIN-DOWNLOAD") Then
                ProcessDownload(Request, MySocket)
            Else
                If Request.Length = 0 Or Request = "/" Then
                    Request = DefaultDocument
                End If
                Request = Request.Replace("/", "\")
                PushLog(Request, MySocket.RemoteEndPoint.ToString, eLogType.Access)
                PhysicalFilePath = VirtualRoot.TrimEnd("\") & "\" & Request.TrimStart("\")
                ProcessFile(PhysicalFilePath, MySocket, False)
            End If
        End If
        MySocket.Close()
    End Sub

    'Processors

    Private Sub ProcessPost(ByVal Header As List(Of Byte), ByVal MySocket As Socket, ByVal HeaderRead As Long)
        Dim objPost As New PostParser()

        objPost.Header = Header

        Dim Buffer As Integer = 2194304
        Dim BB(Buffer - 1) As Byte

        Dim NetStream As New NetworkStream(MySocket)

        Dim TotalRead As UInt64 = HeaderRead - objPost.HeaderLenght
        Dim CurrentRead As Integer = 0
        While TotalRead < objPost.ContentLength
            CurrentRead = NetStream.Read(BB, 0, Buffer)
            TotalRead += CurrentRead
            objPost.PushData(BB.Take(CurrentRead).ToList)
        End While
        objPost.Close()

        Dim objResponse As New Response(MySocket)
        objResponse.SendStatusResponse(eStatusCode.STATUS200_OK)
        objResponse.AppendHeader(eHttpHeader.Content_Length, "{""success"":true}".Length)
        objResponse.AppendHeader(eHttpHeader.Content_Type, "text/html")
        objResponse.AppendHeader(eHttpHeader.Server, Response.SERVER_NAME)
        'objResponse.AppendHeader(eHttpHeader.Accept_Ranges, "bytes")
        objResponse.Write("{""success"":true}")
        objResponse.Close()
    End Sub

    Private Sub ProcessDownload(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
            Dim FileName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "fileName")
            RootFolder = MobiminLib.FileManager.GetPhysicalPath(RootFolder)
            Dim PhysicalFile = MobiminLib.FileManager.CombinePath(RootFolder, FileName)
            ProcessFile(PhysicalFile, mySocket, True)
        Catch ex As Exception
            PushLog("Error in ProcessDownload: " & ex.Message, "", eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessAjax(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim AjaxCommand = MobiminLib.RequestUtil.GetCommand(Request)
            Select Case AjaxCommand
                Case "CALLHISTORY"
                    ProcessCallHistory(Request, mySocket)
                Case "MISSEDCALLS"
                    ProcessMissedCalls(Request, mySocket)
                Case "INCOMINGCALLS"
                    ProcessIncomingCalls(Request, mySocket)
                Case "OUTGOINGCALLS"
                    ProcessOutgoingCalls(Request, mySocket)
                Case "CAMERA"
                    ProcessCamera(Request, mySocket)
                Case "STATUS"
                    ProcessStatus(Request, mySocket)
                Case "FOLDERS"
                    ProcessFolders(Request, mySocket)
                Case "FILES"
                    ProcessFolderFiles(Request, mySocket)
                Case "FILEMANAGER"
                    ProcessFileManager(Request, mySocket)
                Case "REGISTRY"
                    ProcessRegistry(Request, mySocket)
                Case "REGISTRYKEYS"
                    ProcessRegistryKeys(Request, mySocket)
                Case "REGISTRYMANAGER"
                    ProcessRegistryManager(Request, mySocket)
                Case "OUTLOOK"
                    ProcessOutlook(Request, mySocket)
            End Select

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ProcessOutlook(ByVal Request As String, ByVal mySocket As Socket)
        Dim Action = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "action")
        Select Case Action
            Case "contacts"
                ProcessContacts(mySocket)
            Case "contactphoto"
                ProcessContactPhoto(Request, mySocket)
            Case "appointments"
                ProcessAppointments(mySocket)
            Case "tasks"
                ProcessTasks(mySocket)
            Case "categories"
                ProcessCategories(mySocket)
            Case "newcontact"
                ProcessNewContact(Request, mySocket)
            Case "deletecontact"
                ProcessDeleteContact(Request, mySocket)
        End Select
    End Sub

    Private Sub ProcessDeleteContact(ByVal Request As String, ByVal mySocket As Socket)
        Dim ItemId = RequestUtil.GetQuery(Of String)(Request, "itemId")
        Dim objOL As New MobiminLib.Outlook
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(objOL.DeleteContact(ItemId))
    End Sub

    Private Sub ProcessNewContact(ByVal Request As String, ByVal mySocket As Socket)
        Dim Contact As New MobiminLib.Contact

        Dim Anni = RequestUtil.GetQuery(Of String)(Request, "anniversary")
        If Not String.IsNullOrEmpty(Anni) Then
            Contact.Anniversary = MobiminLib.Util.ToLocalDate(Anni)
        End If

        Dim BirthDay = RequestUtil.GetQuery(Of String)(Request, "birthday")
        If Not String.IsNullOrEmpty(BirthDay) Then
            Contact.Birthday = MobiminLib.Util.ToLocalDate(BirthDay)
        End If

        Contact.Category = RequestUtil.GetQuery(Of String)(Request, "categories")
        Contact.Company = RequestUtil.GetQuery(Of String)(Request, "company")
        Contact.Department = RequestUtil.GetQuery(Of String)(Request, "department")
        Contact.FirstName = RequestUtil.GetQuery(Of String)(Request, "firstName")
        Contact.HomeCity = RequestUtil.GetQuery(Of String)(Request, "homeCity")
        Contact.HomeCountry = RequestUtil.GetQuery(Of String)(Request, "homeCountry")
        Contact.HomeFax = RequestUtil.GetQuery(Of String)(Request, "homeFax")
        Contact.HomePhone = RequestUtil.GetQuery(Of String)(Request, "homePhone")
        Contact.HomeState = RequestUtil.GetQuery(Of String)(Request, "homeState")
        Contact.HomeStreet = RequestUtil.GetQuery(Of String)(Request, "homeStreet")
        Contact.HomeZip = RequestUtil.GetQuery(Of String)(Request, "homeZip")
        Contact.JobTitle = RequestUtil.GetQuery(Of String)(Request, "jobTitle")
        Contact.LastName = RequestUtil.GetQuery(Of String)(Request, "lastName")
        Contact.MainEmail = RequestUtil.GetQuery(Of String)(Request, "mainEmail")
        Contact.Middel = RequestUtil.GetQuery(Of String)(Request, "middle")
        Contact.Mobile = RequestUtil.GetQuery(Of String)(Request, "mobile")
        Contact.Notes = RequestUtil.GetQuery(Of String)(Request, "notes")
        Contact.WebPage = RequestUtil.GetQuery(Of String)(Request, "webPage")
        Contact.WorkCity = RequestUtil.GetQuery(Of String)(Request, "workCity")
        Contact.WorkCountry = RequestUtil.GetQuery(Of String)(Request, "workCountry")
        Contact.WorkEmail = RequestUtil.GetQuery(Of String)(Request, "workEmail")
        Contact.WorkFax = RequestUtil.GetQuery(Of String)(Request, "workFax")
        Contact.WorkPhone = RequestUtil.GetQuery(Of String)(Request, "workTel")
        Contact.WorkState = RequestUtil.GetQuery(Of String)(Request, "workState")
        Contact.WorkStreet = RequestUtil.GetQuery(Of String)(Request, "workStreet")
        Contact.WorkZip = RequestUtil.GetQuery(Of String)(Request, "workZip")
        Contact.IM = RequestUtil.GetQuery(Of String)(Request, "im")

        Dim objOutlook As New MobiminLib.Outlook
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(objOutlook.SaveContact(Contact))
    End Sub

    Private Sub ProcessCategories(ByVal mySocket As Socket)
        Dim objOutlook As New MobiminLib.Outlook
        Dim jCategories = objOutlook.GetCategories
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(jCategories)
        objResponse.Close()
    End Sub

    Private Sub ProcessTasks(ByVal mySocket As Socket)
        Dim objOutlook As New MobiminLib.Outlook
        Dim jTasks = objOutlook.GetTasks
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(jTasks)
        objResponse.Close()
    End Sub

    Private Sub ProcessContactPhoto(ByVal Request As String, ByVal mySocket As Socket)
        Dim objOutlook As New MobiminLib.Outlook
        Dim ItemId = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "itemId")
        Dim ContactPhoto = objOutlook.GetContactPhoto(ItemId)

        If ContactPhoto IsNot Nothing Then
            Dim MemStream As New MemoryStream
            ContactPhoto.Save(MemStream, Imaging.ImageFormat.Jpeg)
            'Dim SReader As New StreamReader(MemStream)
            MemStream.Seek(0, SeekOrigin.Begin)
            Dim B(MemStream.Length - 1) As Byte
            MemStream.Read(B, 0, MemStream.Length)
            Dim objResponse As New Response(mySocket)
            objResponse.CommonHeaders(MemStream.Length, "image/jpeg")
            objResponse.Write(B)
            objResponse.Close()
            ContactPhoto.Dispose()
        Else
            Dim NoPhotoPath As String
            NoPhotoPath = MobiminLib.Util.GetWWWFolder(MobiminLib.eWWWFolder.WWWROOT)
            NoPhotoPath = MobiminLib.FileManager.CombinePath(NoPhotoPath, "images\nophoto.png")
            ProcessFile(NoPhotoPath, mySocket, False)
        End If

    End Sub

    Private Sub ProcessAppointments(ByVal mySocket As Socket)
        Dim objOutlook As New MobiminLib.Outlook
        Dim jApp = objOutlook.GetAppointments
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(jApp)
        objResponse.Close()
    End Sub

    Private Sub ProcessContacts(ByVal mySocket As Socket)
        Dim objOutlook As New MobiminLib.Outlook
        Dim jContacts = objOutlook.GetContacts
        Dim objResponse As New Response(mySocket)
        objResponse.WriteJson(jContacts)
        objResponse.Close()
    End Sub

    Private Sub ProcessFile(ByVal PhysicalFilePath As String, ByVal mySocket As Socket, ByVal ForceDownload As Boolean)
        Dim ErrorMessage As String
        Dim MimeType As String
        Dim objResponse As New Response(mySocket)
        If Not File.Exists(PhysicalFilePath) Then
            ErrorMessage = "404 Error! File Does Not Exists..."
            'SendHeader(HttpVersion, "", ErrorMessage.Length, " 404 Not Found", mySocket, ForceDownload)
            objResponse.SendStatusResponse(eStatusCode.STATUS404_Not_Found)
            objResponse.AppendHeader(eHttpHeader.Server, Response.SERVER_NAME)
            objResponse.AppendHeader(eHttpHeader.Content_Type, "text/html")
            objResponse.AppendHeader(eHttpHeader.Content_Length, ErrorMessage.Length)
            'objResponse.AppendHeader(eHttpHeader.Accept_Ranges, "bytes")
            objResponse.Write(ErrorMessage)
            Dim ClientIP = mySocket.RemoteEndPoint.ToString
            objResponse.Close()
            'SendToBrowser(HttpVersion, mySocket)
            PushLog("Request not found: " & PhysicalFilePath, ClientIP, eLogType.Warning)
        Else

            Try
                'Dim iTotBytes As Integer = 0
                Dim sResponse As String = ""
                Dim fs As New FileStream(PhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
                Dim reader As New BinaryReader(fs)
                Dim BufferSize = 2048576 'to prevent out of memory
                Dim bytes() As Byte = New Byte(BufferSize - 1) {}

                If ForceDownload Then
                    MimeType = "application/octet-stream"
                Else
                    MimeType = GetMimeType(PhysicalFilePath)
                End If
                objResponse.CommonHeaders(fs.Length, MimeType)

                If ForceDownload Then
                    objResponse.AppendHeader(eHttpHeader.Content_Disposition, String.Format("attachment; filename={0}", New IO.FileInfo(PhysicalFilePath).Name))
                End If

                Dim NbBytes As Integer = 0

                While reader.BaseStream.Position < reader.BaseStream.Length
                    NbBytes = reader.Read(bytes, 0, bytes.Length)
                    If NbBytes < BufferSize Then
                        ReDim Preserve bytes(NbBytes - 1)
                    End If
                    objResponse.Write(bytes)
                End While
                reader.Close()
                fs.Close()
                objResponse.Close()
            Catch ex As Exception
                PushLog("Error Processing the request: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
                ErrorMessage = "Unknow Error"
                objResponse.SendStatusResponse(eStatusCode.STATUS500_Internal_Server_Error)
                objResponse.AppendHeader(eHttpHeader.Server, Response.SERVER_NAME)
                objResponse.Close()
            End Try
        End If
    End Sub

    Private Sub ProcessCamera(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objCamera As New MobiminLib.Camera
            Dim ImageWidth As Integer
            Dim ImageHeight As Integer
            Dim FileName As String = "{0}.jpg"

            objCamera.Init(MobiminLib.Util.GetWWWFolder(MobiminLib.eWWWFolder.CAPTURES))
            FileName = String.Format(FileName, System.Guid.NewGuid.ToString)
            Dim StoredFile As String = objCamera.Capture(FileName)

            Using CapturedImage As New Bitmap(StoredFile)
                ImageWidth = CapturedImage.Width
                ImageHeight = CapturedImage.Height
            End Using

            Dim Response As String = "{{""src"":""<img src='Captures/{0}' />"", ""width"":{1}, ""height"":{2}}}"
            Response = String.Format(Response, FileName, ImageWidth, ImageHeight)
            objResponse.CommonHeaders(Response.Length, "text/html")
            objResponse.Write(Response)
            objResponse.Close()
        Catch ex As Exception
            PushLog("Error in ProcessCamera: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessCallHistory(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objCallHistory As New MobiminLib.CallHistory
            Dim Start As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim jCallHistory = objCallHistory.GetCallHistory(Start, Limit)
            objResponse.WriteJson(jCallHistory)
        Catch ex As Exception
            PushLog("Error in ProcessCallHistory: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessMissedCalls(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objMissedCalls As New MobiminLib.CallHistory
            Dim Start As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim jMissedCalls = objMissedCalls.GetMissedCalls(Start, Limit)
            objResponse.WriteJson(jMissedCalls)
        Catch ex As Exception
            PushLog("Error in ProcessMissedCalls: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessIncomingCalls(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objIncomingCalls As New MobiminLib.CallHistory
            Dim Start As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim jIncomingCalls = objIncomingCalls.GetIncomingCalls(Start, Limit)
            objResponse.WriteJson(jIncomingCalls)
        Catch ex As Exception
            PushLog("Error in ProcessIncomingCalls: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessOutgoingCalls(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objOutgoingCalls As New MobiminLib.CallHistory
            Dim Start As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit As Integer = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim jOutgoingCalls = objOutgoingCalls.GetOutgoingCalls(Start, Limit)
            objResponse.WriteJson(jOutgoingCalls)
        Catch ex As Exception
            PushLog("Error in ProcessOutgoingCalls: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessStatus(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objStatus As New MobiminLib.Settings
            Dim jStatus As String = objStatus.GetStatus
            objResponse.WriteJson(jStatus)
        Catch ex As Exception
            PushLog("Error in ProcessStatus: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessRegistry(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objRegistryEditor As New MobiminLib.RegistryEditor
            Dim Root As String = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "node")
            Dim jRegistry As String = objRegistryEditor.GetNode(Root)
            objResponse.WriteJson(jRegistry)
        Catch ex As Exception
            PushLog("Error in ProcessRegistry: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessRegistryKeys(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objRegistryEditor As New MobiminLib.RegistryEditor
            Dim Root As String = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "root")
            Dim Start = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim jRegistry As String = objRegistryEditor.GetSubKeysValues(Root, Start, Limit)
            objResponse.WriteJson(jRegistry)
        Catch ex As Exception
            PushLog("Error in ProcessRegistry: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessRegistryManager(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim Action = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "action")
            Dim Root = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentNode")
            Dim Response As String = "{""success"":false,""msg"":""Unknown Error""}"
            Dim objRegistryEditor As New MobiminLib.RegistryEditor
            Select Case Action
                Case "newkey"
                    Dim Data = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Response = objRegistryEditor.NewKey(Root, Data)
                Case "newvalue"
                    Dim ValueData = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "valueData")
                    Dim ValueName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "valueName")
                    Dim Kind = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "kind")
                    Dim Extra = MobiminLib.RequestUtil.GetQuery(Of Boolean)(Request, "extra")
                    Response = objRegistryEditor.NewValue(Root, ValueName, ValueData, Kind, Extra)
                Case "delete"
                    Dim Data = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim Kind = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "kind")
                    Response = objRegistryEditor.DeleteRegistry(Root, Data, Kind)
            End Select
            Dim objResponse As New Response(mySocket)
            objResponse.WriteJson(Response)
        Catch ex As Exception
            PushLog("Error in ProcessRegistry: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessFolders(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objFileSystem As New MobiminLib.FileManager
            Dim Root As String = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "node")
            Dim jFileSystem As String = objFileSystem.GetFolders(Root)
            objResponse.WriteJson(jFileSystem)
        Catch ex As Exception
            PushLog("Error in ProcessFolders: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessFolderFiles(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim objResponse As New Response(mySocket)
            Dim objFileSystem As New MobiminLib.FileManager
            Dim Root As String = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "root")
            Dim Start = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "start")
            Dim Limit = MobiminLib.RequestUtil.GetQuery(Of Integer)(Request, "limit")
            Dim SortField = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "sortField")
            Dim Direction = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "direction")
            Dim jFileSystem As String = objFileSystem.GetFiles(Root, Start, Limit, SortField, Direction)
            objResponse.WriteJson(jFileSystem)
        Catch ex As Exception
            PushLog("Error in ProcessFiles: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub ProcessFileManager(ByVal Request As String, ByVal mySocket As Socket)
        Try
            Dim Action = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "action")
            Dim Response As String = "{""success"":false}"
            Dim objFileManager As New MobiminLib.FileManager
            Select Case Action.ToLower
                Case "newfolder"
                    Dim NewFolderName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Response = objFileManager.CreateFolder(RootFolder, NewFolderName)
                Case "deletefolder"
                    Dim FolderName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Response = objFileManager.DeleteFolder(RootFolder, FolderName)
                Case "deletefile"
                    Dim FileName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Response = objFileManager.DeleteFile(RootFolder, FileName)
                Case "renamefolder"
                    Dim FolderName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Dim NewName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "newName")
                    Response = objFileManager.RenameFolder(RootFolder, FolderName, NewName)
                Case "renamefile"
                    Dim FileName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Dim NewName = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "newName")
                    Response = objFileManager.RenameFile(RootFolder, FileName, NewName)
                Case "property"
                    Dim Data = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "data")
                    Dim RootFolder = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "parentFolder")
                    Dim Type = MobiminLib.RequestUtil.GetQuery(Of String)(Request, "type")
                    Response = objFileManager.GetProperties(RootFolder, Data, Type)
            End Select
            Dim objResponse As New Response(mySocket)
            objResponse.WriteJson(Response)
        Catch ex As Exception
            PushLog("Error in ProcessFileManager: " & ex.Message, mySocket.RemoteEndPoint.ToString, eLogType.Error)
        End Try
    End Sub

    Private Sub PushLog(ByVal Message As String, ByVal ClientIP As String, ByVal LogType As eLogType)

    End Sub

End Class
